//
// winter - Direct2D GUI framework.
// Copyright (C) 2009 Aleksandar Dezelin 
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef MESSAGE_PUMP_H
#define MESSAGE_PUMP_H

#pragma once
#include <Windows.h>

namespace winter {
namespace platform {

///
/// <summary>
///     Implements a simple thread message pump.
/// </summary>
///
struct message_pump {

  ///
  /// <summary>
  ///     Runs a message dispatching loop.
  /// </summary>
  /// <param name="hwnd">
  ///     Handle of the window to dispatch messages for. 
  ///     If it's NULL, then run() will dispatch messages for 
  ///     all thread windows.
  /// </param>
  /// <returns>
  ///     The error code returned from WM_QUIT.
  /// </returns>
  ///
  int run(HWND hwnd = NULL) {
    MSG message;
    while(::GetMessage(&message, hwnd, 0, 0) > 0) {
      ::TranslateMessage(&message);
      ::DispatchMessage(&message);
    }

    return static_cast<int>(message.wParam);
  }
};

}
}

#endif // MESSAGE_PUMP_H
